<html>
<body>
Text Size
<input size="15" id="ts" value="" onpropertychange="fn();" oninput="fn();"/> 
<button class="btns">连接蓝牙</button>
<br/>
<br/>
Text Height:
<h1 size="15" id="th"/>
<script type="text/javascript">
    var ts= document.getElementById("ts");
    var th = document.getElementById("th");
    var sizeArray = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 192, 195, 198, 201, 204, 207, 210, 213, 216, 219, 222, 225, 228, 231, 234, 237, 240, 243, 246, 249, 252, 255, 258, 261, 264, 267, 270, 273, 276, 279, 282, 285, 288, 291, 294, 297, 300, 303, 306, 309, 312, 315, 318, 321, 324, 327, 330, 333, 336, 339, 342, 345, 348, 351, 354, 357, 360, 363, 366, 369, 372, 375, 378, 381, 384, 387, 390, 393, 396, 399, 402, 405, 408, 411, 414, 417, 420, 423, 426, 429, 432, 435, 438, 441, 444, 447, 450, 453, 456, 459, 462, 465, 468, 471, 474, 477, 480, 483, 486, 489, 492, 495, 498, 501, 504, 507, 510, 513, 516, 519, 522, 525, 528, 531, 534, 537, 540, 543, 546, 549, 552, 555, 558, 561, 564, 567, 570, 573, 576, 579, 582, 585, 588, 591, 594, 597, 600, 603, 606, 609, 612, 615, 618, 621, 624, 627, 630, 633, 636, 639, 642, 645, 648, 651, 654, 657, 660, 663, 666, 669, 672, 675, 678, 681, 684, 687, 690, 693, 696, 699, 702, 705, 708, 711, 714, 717, 720, 723, 726, 729, 732, 735, 738, 741, 744, 747, 750, 753, 756, 759, 762, 765, 768, 771, 774];
var heightArray= [5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 44, 49, 53, 57, 61, 65, 68, 73, 77, 81, 85, 88, 92, 97, 101, 105, 108, 112, 116, 121, 125, 129, 132, 136, 140, 145, 149, 152, 156, 160, 164, 169, 172, 176, 180, 184, 188, 193, 196, 200, 204, 208, 212, 216, 220, 224, 228, 232, 235, 240, 244, 248, 252, 256, 259, 264, 268, 272, 276, 279, 283, 288, 292, 296, 299, 303, 307, 312, 316, 320, 323, 327, 331, 336, 340, 343, 347, 351, 355, 360, 363, 367, 371, 375, 380, 384, 387, 391, 395, 399, 404, 407, 411, 415, 419, 423, 427, 431, 435, 439, 443, 447, 451, 455, 459, 463, 467, 470, 475, 479, 483, 487, 490, 494, 499, 503, 507, 511, 514, 518, 523, 527, 531, 534, 538, 542, 547, 551, 555, 558, 562, 566, 571, 575, 578, 582, 586, 590, 595, 598, 602, 606, 610, 614, 619, 622, 626, 630, 634, 638, 642, 646, 650, 654, 658, 661, 666, 670, 674, 678, 682, 685, 690, 694, 698, 702, 705, 709, 714, 718, 722, 725, 729, 733, 738, 742, 746, 749, 753, 758, 762, 766, 769, 773, 777, 782, 786, 789, 793, 797, 801, 806, 810, 813, 817, 821, 825, 830, 833, 837, 841, 845, 849, 853, 857, 861, 865, 869, 873, 877, 881, 885, 889, 893, 896, 901, 905, 909, 913, 916, 920, 925, 929, 933, 937, 940, 944, 949, 953, 957, 960, 964, 968, 973, 977, 980, 984, 988, 992, 997, 1001, 1004, 1008, 1012, 1016, 1021, 1024, 1028];
    function fn() {
        var value = parseInt(ts.value);
        var index = sizeArray.indexOf(value);
        if (index < 0) {
	th.innerHTML=parseInt(value * 1.35 + 0.5);
        } else {
                th.innerHTML=heightArray[index];
        }
    }
 var bluetoothDevice;
        let btn = document.querySelector('.btns')
        btn.addEventListener('click', getBlueObject)
        // 获取蓝牙对象
        function getBlueObject() {
            bluetoothDevice = null;
            // 这一块的筛选条件在 推荐的网站1里面可以粗略的学习到
            navigator.bluetooth.requestDevice({
                filters: [{  namePrefix: 'HUAWEI' } ]
            }).then(device => {
                bluetoothDevice = device
                // 监听蓝牙蓝牙断开
                bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected)
                connect()
            }).catch(err => {
                console.log('获取蓝牙失败');
            })
        }
        // 开始连接
        function connect() {
            exponentialBackoff(3 /* 最大重试次数 */, 2 /* 秒延迟 */,
                function toTry() {
                    return bluetoothDevice.gatt.connect();
                },
                function success(server) {
                    getServer(server)
                },
                function fail() {
                    console.log('连接失败');
                });
        }
        // 获取服务\特征,监听蓝牙数据返回
        function getServer(server) {
            console.log('连接成功', server);
            server.getPrimaryService('00001810-0000-1000-8000-00805f9b34fb').then(service => {
                console.log('获取全部服务信息成功,开始获取特征信息', service)
                return service.getCharacteristic('00002a35-0000-1000-8000-00805f9b34fb')
            }).then(characteristic => {
                return characteristic.startNotifications().then(_ => {
                    characteristic.addEventListener('characteristicvaluechanged',
                        handleNotifications);
                });
            }).catch(err => {
                console.log('获取服务失败', err);
            })
        }
        function onDisconnected(max, delay, status) {
            console.log('重连中');
            connect()
        }
        function exponentialBackoff(max, delay, toTry, success, fail) {
            toTry().then(result => success(result))
                .catch(_ => {
                    if (max === 0) {
                        return fail();
                    }
                    setTimeout(function () {
                        exponentialBackoff(--max, delay * 2, toTry, success, fail);
                    }, delay * 1000);
                });
        }
        function handleNotifications(event) {
            let value = event.target.value;
            let a = [];
            for (let i = 0; i < value.byteLength; i++) {
                a.push('0x' + ('00' + value.getUint8(i).toString(16)).slice(-2));
            }
            console.log('> ' + a.join(' '));
        }
</script>
</body>
</html>